/*
 *  RGBADescriptor.cpp
 *  libavid
 *
 *  Created by Peter on 27.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "RGBADescriptor.h"
#include "AStream.h"

A_IMPLEMENT_CLASS( 'RGBA', RGBADescriptor, DIDDescriptor );

void RGBADescriptor::readFromStream( AStream& str, const AObjDoc& objd )
{
	DIDDescriptor::readFromStream( str, objd );
	
	UInt8 tagMark, version;
	
	str.readPod( tagMark );
	str.readPod( version );
	
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 1 );
	
	str.readPod( _pixelLayoutSize );
	TR_ASSERT( _pixelLayoutSize >= 0 );
	_pixelLayout = _pixelLayoutSize > 0 ? new UInt8[ _pixelLayoutSize ] : nullptr;
	str.readBuffer( _pixelLayout, _pixelLayoutSize );
	
	str.readPod( _pixelStructureSize );
	TR_ASSERT( _pixelStructureSize >= 0 );
	_pixelStructure = _pixelStructureSize > 0 ? new UInt8[ _pixelStructureSize ] : nullptr;
	str.readBuffer( _pixelStructure, _pixelLayoutSize );
	
	str.readPod( _paletteLayoutSize );
	TR_ASSERT( _paletteLayoutSize == 0 ); // Don't know 100% certain how to deal with this since it hasn't come up yet. Suspect it's similar to pixel layout/structure, though.
	
	str.readPod( _paletteStructureSize );
	TR_ASSERT( _paletteStructureSize == 0 ); // Anything else is not implemented since we don't have example data
	
	str.readPod( _paletteSize );
	TR_ASSERT( _paletteSize == 0 ); // Don't know 100% for sure how to deal with other values
	
	UInt8 endTagMark;
	str.readPod( endTagMark );
	TR_ASSERT( endTagMark == 0x03 );
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl

template< typename T >
static void dumpArray ( 
   std::ostream& str, 
   const char* name,
   const T* t,
   std::size_t size
){
	str << name << "[ " << size << " ] {" << std::endl;
	for( int i = 0; i < size; ++i ) {
		str << static_cast< long >( t[ i ] );
		if( i < size - 1 )
			str << ", ";
		
		str << std::endl;
	}
	str << "}" << std::endl;
}

void RGBADescriptor::dump( std::ostream& str ) const
{
	str << "RGBADescriptor {" << std::endl;
	
	str << "\t";
	DIDDescriptor::dump( str );
	
	str << TRACE_FIELD( _pixelLayoutSize );
	dumpArray( str, "_pixelLayout", _pixelLayout, _pixelLayoutSize );
	
	str << TRACE_FIELD( _pixelStructureSize );
	dumpArray( str, "_pixelStructure", _pixelStructure, _pixelStructureSize );
	
	str << TRACE_FIELD( _paletteLayoutSize );
	str << TRACE_FIELD( _paletteStructureSize );
	str << TRACE_FIELD( _paletteSize );
	
	str << "}" << std::endl;
}
